﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jzy.Model.Mail;
using System.Data;
using Jzy.Model;
using Frame.Lib.Core.Base;
using Frame.Lib.Utilities;


namespace Jzy.DAL.Mail
{
    public class MailDAL:BaseDAL
    {
        private int userID;

        public MailDAL(int userID)            
        {
            this.userID = userID;
        }

        public DataTable GetList(int folderID,MailStatus status)
        {
            string strSql = "select a.ID as MailID,b.ID ,Name,CreatorName,RecieveNames,InputTime,Status,(select count(*) from " + OTableName.MailAttach + " where BindID = a.ID) as AttachCount from " + OTableName.Mail + " a,MailUser b where a.ID = b.MailID and UserID=" + userID + " and FolderID = " + folderID;
            if (status != MailStatus.All)
            {
                if (status == MailStatus.UnRead)
                {
                    strSql += " and Status = 0";
                }
                else
                {
                    strSql += " and Status & " + (int)status + " = " + (int)status;
                }
            }

            strSql += " order by InputTime desc";
            DataTable dt = dbOper.Query(strSql);
            return dt;
        }

        public void Move(List<string> lstMailUserIDs,object destFolder)
        {
            string strMailUserIDs = ConvertUtil.ConvertToDbStr(lstMailUserIDs);
            string strSql = "update " + OTableName.MailUser + " set FolderID = " + destFolder + " where ID in (" + strMailUserIDs + ")";
            dbOper.RunSql(strSql);
        }

        public void Send(int mailID, List<int> lstRecieveIDs)
        {
            string[] strSqls = new string[lstRecieveIDs.Count + 1];
            strSqls[0] = "insert into " + OTableName.MailUser + "(MailID, UserID, FolderID ,Status) values (" + mailID + "," + userID + "," + (int)MailSysFolder.已发送 + ",1)";//保存自己的已发邮件
            for (int i = 0; i < lstRecieveIDs.Count;i++ )
            {
                strSqls[i + 1] = "insert into " + OTableName.MailUser + "(MailID, UserID, FolderID) values(" + mailID + "," + lstRecieveIDs[i] + "," + (int)MailSysFolder.收件箱 + ")";//发送到收件人
            }
            dbOper.RunSql(strSqls);
        }

        public void DeleteBoxMail(string mailUserID)
        {
            string strSql = "delete from " + OTableName.MailUser + " where ID=@p0";
            dbOper.RunSql(strSql, mailUserID);
        }

        public void DeleteBoxMail(List<string> lstMailUserIDs)
        {
            string strMailUserIDs = ConvertUtil.ConvertToDbStr(lstMailUserIDs);
            string strSql = "delete from " + OTableName.MailUser + " where ID in (" + strMailUserIDs + ")";
            dbOper.RunSql(strSql);
        }

        /// <summary>
        /// 就用于从草稿箱里发邮件时删除草稿箱里原来的邮件
        /// </summary>
        /// <param name="folderID"></param>
        /// <param name="mailID"></param>
        public void DeleteBoxMail(int folderID, int mailID)
        {
            string strSql = "delete from " + OTableName.MailUser + " where UserID=" + userID + " and FolderID = @p0 and MailID = @p1";
            dbOper.RunSql(strSql, folderID, mailID);
        }

        public void SaveToDraft(int mailID)
        {
            string strSql = "insert into " + OTableName.MailUser + "(MailID,UserID,FolderID) values(@p0," + userID + "," + (int)MailSysFolder.草稿箱 + ")";
            dbOper.RunSql(strSql, mailID);
        }

        public byte GetStatus(int folderID, int mailID)
        {
            string strSql = "select Status from " + OTableName.MailUser + " where UserID = " + userID + " and FolderID=" + folderID + " and MailID =" + mailID;
            byte chStatus = dbOper.GetScalar<byte[]>(strSql)[0];
            return chStatus;
        }

        public byte GetStatus(string mailUserID)
        {
            string strSql = "select Status from " + OTableName.MailUser + " where ID='" + mailUserID + "'";
            byte status = dbOper.GetScalar<byte[]>(strSql)[0];
            return status;
        }

        public void SetStatus(int folderID, int mailID, MailStatus status)
        {
            string strSql = "update " + OTableName.MailUser + " set Status = Status | " + (int)status + " where UserID=" + userID + " and FolderID=" + folderID + " and MailID=" + mailID;
            dbOper.RunSql(strSql);
        }
        public void SetStatus(string mailUserID, MailStatus status)
        {
            string strSql = "update " + OTableName.MailUser + " set Status = Status | " + (int)status + " where ID='" + mailUserID + "'";
            dbOper.RunSql(strSql);
        }

        public int GetMailID(string mailUserID)
        {
            string strSql = "select MailID from " + OTableName.MailUser + " where ID='" + mailUserID + "'";
            int mailID = dbOper.GetScalar<int>(strSql);
            return mailID;
        }

        public int GetUnreadMailCount()
        {
            string strSql = "select count(MailID) from " + OTableName.MailUser + " where UserID = " + userID + " and FolderID = " + (int)MailSysFolder.收件箱 + " and Status | " + (int)MailStatus.UnRead + " = " + (int)MailStatus.UnRead;
            int count = dbOper.GetScalar<int>(strSql);
            return count;
        }

        public void Clear(object folderID)
        {
            string sql = "delete from " + OTableName.MailUser + " where UserID = " + userID + " and FolderID = " + folderID;
            dbOper.RunSql(sql);
        }

       
    }
}
